package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.Supplier;
import com.ruoyi.project.drug.service.ISupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/drug/supplier")
public class SupplierController extends BaseController {
    @Autowired
    private ISupplierService iSupplierService;
    /**查询所有供应商信息*/
    @PreAuthorize("@ss.hasPermi('drug:supplier:query')")
    @GetMapping("/list")
    public TableDataInfo list(Supplier supplier){
        startPage();
        return getDataTable(iSupplierService.getSuppliers(supplier));
    }

    /**添加供应商信息*/
    @PreAuthorize("@ss.hasPermi('drug:supplier:add')")
    @PostMapping("/add")
    public AjaxResult addSupplier(@RequestBody Supplier supplier){
        supplier.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iSupplierService.insertSupplier(supplier));
    }

    /**根据ID查询供应商的信息*/
    @PreAuthorize("@ss.hasPermi('drug:supplier:query')")
    @GetMapping("/{supId}")
    public AjaxResult getById(@PathVariable Long supId){
        Supplier supplier= iSupplierService.getById(supId);
        return AjaxResult.success(supplier);
    }

    @PutMapping
    @PreAuthorize("@ss.hasPermi('drug:supplier:edit')")
    public AjaxResult editSupplier(@RequestBody Supplier supplier){
        supplier.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iSupplierService.alterSupplier(supplier));
    }
    /**向供应商提供删除功能*/
    @DeleteMapping("/{supIds}")
    @PreAuthorize("@ss.hasPermi('drug:supplier:remove')")
    public AjaxResult removeSupplier(@PathVariable Long[] supIds){
        return toAjax(iSupplierService.deleteSupplier(supIds));
    }

    /**为库存管理提供下拉菜单选项*/
    @GetMapping("/supList")
    @PreAuthorize("@ss.hasPermi('drug:supplier:query')")
    public AjaxResult supList(){
        return AjaxResult.success(iSupplierService.menuSupplier());
    }


}
